<template>
  <div class="app-container clearfix">  
  <el-row>
    <el-col :span="19">
      <div style="padding:25px;">
      账户资产：￥{{accountData.amount}}
      账户余额：￥{{accountData.cash}}
      资产：￥{{accountData.capital}}
      利润：￥{{accountData.profit}}
      市值：￥{{accountData.marketValue}}
      </div>
    </el-col>
    <el-col :span="5">
      <div style="padding:25px; text-align:right">
      最后更新：{{accountData.updatedAt}}
      </div>
      </el-col>
  </el-row>
  <el-row>
    <el-col :span="9">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>账户资金走势</span>
            <div id="lineBarBox" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>        

      </div>
      
    </el-col>
    <el-col :span="5">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>费用分布</span>
            <div id="lineBarBoxb" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>     

      </div>   
      
    </el-col>
    <el-col :span="5">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>资产分布</span>
            <div id="lineBarBoxc" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>      

      </div>
      
    </el-col>
    <el-col :span="5">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>渠道来源</span>
            <div id="lineBarBoxd" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>        

      </div>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="17" style="text-algin:right;">
      <div style="padding:25px; text-align:right; line-height:36px;">
      统计周期：{{filterForm.beginDate}}/{{filterForm.endDate}}
      </div>
    </el-col>
    <el-col :span="7">
      <div style="padding:25px 0;">
      <el-radio-group v-model="filterForm.scale">
        <el-radio-button label="0">今日</el-radio-button>
        <el-radio-button label="1">昨日</el-radio-button>
        <el-radio-button label="7">近7日</el-radio-button>
        <el-radio-button label="30">近30日</el-radio-button>
        <el-radio-button label="-1">自定义</el-radio-button>
      </el-radio-group>
      </div>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="24">
      <el-table
      :data="turnoverList"
      border
      style="width: 100%">
      <el-table-column
        prop="title"
        label="项目">
      </el-table-column>
      <el-table-column
        label="金额"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="balance"
        label="余额"
        width="100">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="产生时间"
        width="200">
      </el-table-column>
      </el-table>

    </el-col>
  </el-row>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getSymbol} from '@/utils/symbol' // secondary package based on el-pagination
import echarts from 'echarts'
import {getTurnoverList} from '@/api/finance/turnover'
import {getReviewList} from '@/api/finance/review'
import {getAccountDetail} from '@/api/finance/account'

const lineBars = document.getElementById('lineBarBox')
const animationDuration = 6000
export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      turnoverList:[],      
      socket:"",
      tableKey: 0,
      list: [],
      kbarChart:null,
      total: 0,
      listLoading: false,
      feelingsTotal:0,
      accountData:{
        cash:0,
        capital:0,
        profit:0
      },
      filterForm:{
        localSybmol:"",
        scale:'0',
        beginDate:"-",
        size:10,
        page:1,
        endDate:undefined
      },
      columnList:[
        {
          title:"大宗商品"
        },
        {
          title:"政策"
        },
        {
          title:"产业"
        }
      ],
      
      connected:"loading"
    }
  },
  mounted() {
    this.$nextTick(() => {
      //this.initChart()
    })
  },
  watch:{
    "filterForm.scale"(newVal,oldVal){
      newVal = parseInt(newVal)
      console.log(newVal)
      if(newVal >= 0){
        if(newVal == 0){
          //今日
          let dateHandle = new Date()
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        else if(newVal == 1){
          //昨日
          let dateHandle = new Date((new Date()).getTime()-60*60*24*1000)
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        else if(newVal == 7){
          //最近7日
          let dateHandle = new Date((new Date()).getTime()-60*60*24*7*1000)
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        else if(newVal == 30){
          //最近30日
          let dateHandle = new Date((new Date()).getTime()-60*60*24*30*1000)
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        let _dateHandle = new Date()
        this.filterForm.endDate = _dateHandle.getFullYear()+"-"+(_dateHandle.getMonth()+1)+"-"+_dateHandle.getDate()
        console.log(this.filterForm)
        this.getTurnoverList()
        
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    let symbolData = getSymbol()
    try {
      this.filterForm.localSymbol = symbolData.code
    } catch (error) {
      
    }
    let dateHandle = new Date()
    this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
    this.getTurnoverList()

    this.getReviewList()

    this.getAccountDetail()
    
    this.initChart()
  },
  destroyed(){
  },
  methods: {
    initChart() {
      this.chart = echarts.init(lineBars)

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageB',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageC',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [30, 52, 200, 334, 390, 330, 220],
          animationDuration
        }]
      })
    },
    getTurnoverList:function(){
      let _this = this;
      console.log(_this.filterForm)
      getTurnoverList(_this.filterForm).then((response) => {
        console.log(response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    getReviewList:function(){
      let _this = this;
      console.log(_this.filterForm)
      getReviewList(_this.filterForm).then((response) => {
        console.log("review",response)
          if (response.code === 200) {
            this.reviewList = response.data.list
            this.reviewTotal = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    getAccountDetail:function(){
      let _this = this;
      getAccountDetail().then((response) => {
        console.log("getAccountDetail",response)
          if (response.code === 200) {
            this.accountData = response.data
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    }
  }
}
</script>
<style>
.filter { float: left; width: 160px; padding: 20px; min-height: 600px; }
.article { float: left; width: 800px; }
.article-item { width: 800px; height: 200px; margin-right: 25px; }
.article-item-text { float: left; width:600px; }
.article-item-icon { float: right; width:200px; height:150px; padding-left: 50px; }
.article-item-icon img { width:100%; height:100%; border-radius: 10px; }
</style>